package main

import (
    "database/sql"
    "encoding/json"
    "flag"
    "fmt"
    "go.uber.org/zap"
    "google.golang.org/protobuf/proto"
    "laxkits/lib/laxmysql"
    "laxkits/lib/laxutils"
    KKSG "laxkits/lib/ropb"
    "laxkits/lib/zlogger"
)

type XMLNode struct {
    Log zlogger.LaxLogNode `xml:"log"`
    Mysql []laxmysql.LaxMySQLConfig `xml:"mysqlgroup>mysql"`
}
var xmlconfig  XMLNode

var sid=406

func TryQuery() {
    conn := laxmysql.GetLaxMySQLMgr().GetById("1")
    if conn == nil {
        zap.S().Error("获取数据库连接失败")
        return
    }
    sqlstr := "select id, mvp from world.mvprecord where serverid=?"
    if err := conn.Query(sqlstr, func(row *sql.Rows) error {
        var eventid int
        var result []byte
        if err := row.Scan(&eventid, &result); err != nil {
            zap.S().Info("scan失败, ", err)
            return err
        }
        var pb KKSG.MVPRecordPbInfo
        if err := proto.Unmarshal(result, &pb); err != nil {
            zap.S().Info("序列化pb失败:", err)
            return err
        }
        jsonValue, err := json.MarshalIndent(pb, "", "\t")
        if err != nil {
            zap.S().Infof("序列化结果:%s", err);
            return err
        }
        zap.S().Infof("序列化成功:< %s >", jsonValue)
        return nil
    }, sid); err != nil {
        zap.S().Error("出错:", err)
    }
}

func TryModify() {
    conn := laxmysql.GetLaxMySQLMgr().GetById("1")
    if conn == nil {
        return
    }
    sqlstr := "select id, mvp from world.mvprecord where serverid=?"
    conn.Query(sqlstr, func(row *sql.Rows) error {
        var eventid int
        var result []byte
        if err := row.Scan(&eventid, &result); err != nil {
            zap.S().Info("scan失败, ", err)
            return err
        }
        var pb KKSG.MVPRecordPbInfo
        if err := proto.Unmarshal(result, &pb); err != nil {
            zap.S().Info("序列化pb失败:", err)
            return err
        }
        pb.FinalKillerId = 0
        pb.FinalKillerName = "----"
        usql := "update world.mvprecord set mvp = ? where id = ? and serverid=?"
        var err error
        result, err = proto.Marshal(&pb)
        if err != nil {
            zap.S().Info("修改失败", err)
            return err
        }
        var affect int64
        affect, err = conn.Execute(usql, result, eventid, sid)
        if err != nil {
            zap.S().Warn("修改失败", err)
            return err
        }
        zap.S().Info("修改成功，影响", affect)
        return nil
    },sid)
}

func main() {

    var execType string
    flag.StringVar(&execType, "t", "Unknown", "<modify> <query> <sync>")
    flag.Parse()
    fmt.Println("type:", execType)
    if err := laxutils.LoadXmlFile("conf.xml", &xmlconfig); err != nil {
        fmt.Println("加载xml配置失败")
        return
    }
    zlogger.InitLogger(&xmlconfig.Log)
    if err := laxmysql.GetLaxMySQLMgr().RegisterByConfig(xmlconfig.Mysql); err != nil {
        zap.S().Errorf("注册数据库失败:%v", err)
        return
    }
    defer laxmysql.GetLaxMySQLMgr().CloseAll()
    go func() {
        if execType == "modify" {
            TryModify()
        } else if execType == "query" {
            TryQuery()
        } else {
            zap.S().Errorf("参数[%s]非法,合法的有<query> <modify>", execType)
        }
    }()
    laxutils.CloseGracefully(nil)
}
